<?php 
/*
 module:		礼币管理
 create_time:	2022-04-04 16:22:00
 author:	陕西格创
 contact:		
*/

namespace app\api\controller;

use app\api\model\Member as MemberModel;
use app\api\service\ZhVirtualCurrencyService;
use app\api\model\ZhVirtualCurrency as ZhVirtualCurrencyModel;
use app\backend\model\SysSite as SysSiteModel;
use think\exception\ValidateException;
use think\facade\Db;
use think\facade\Log;

class ZhVirtualCurrency extends Common {


	/**
	* @api {get} /ZhVirtualCurrency/index 01、首页数据列表
	* @apiGroup ZhVirtualCurrency
	* @apiVersion 1.0.0
	* @apiDescription  首页数据列表

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (输入参数：) {int}     		[limit] 每页数据条数（默认20）
	* @apiParam (输入参数：) {int}     		[page] 当前页码
	* @apiParam (输入参数：) {int}			[status] 状态 开启|1|success,关闭|0|danger

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据
	* @apiParam (成功返回参数：) {string}     	array.data.list 返回数据列表
	* @apiParam (成功返回参数：) {string}     	array.data.count 返回数据总数
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":" 201","msg":"查询失败"}
	*/
	function index(){
        $wxapp_id     = $this->request->get('wxapp_id');
        $uId          = $this->request->uid;
        if(!$wxapp_id){
            return $this->ajaxReturn($this->errorCode,'缺少平台参数');
        }
        $wxSetting = SysSiteModel::where('wxapp_id',$wxapp_id)->find();
        if(!$wxSetting){
            return $this->ajaxReturn($this->errorCode,'平台参数未配置');
        }

        $user = MemberModel::where(['uid' => $uId, 'wxapp_id' => $wxapp_id])->find();
        if(!$user){
            return $this->ajaxReturn($this->errorCode,'未知用户');
        }
		$limit = $this->request->get('limit', 20, 'intval');
		$page  = $this->request->get('page', 1, 'intval');

		$where             = [];
		$where['status']   = 1;
		$where['wxapp_id'] = $wxapp_id;

		$field = 'id,name';
		$orderby = 'id asc';

		$res = ZhVirtualCurrencyService::indexList(formatWhere($where),$field,$orderby,$limit,$page);
		foreach ($res['list'] as &$v){
            $v['price'] = round($v['name']/$wxSetting['proportion'],2);
        }
		unset($v);
		$res['proportion']               = $wxSetting['proportion'];
		$res['virtual_currency_balance'] = $user['virtual_currency_balance'];
		
		return $this->ajaxReturn($this->successCode,'返回成功',htmlOutList($res));
	}

	/**
	* @api {get} /ZhVirtualCurrency/view 02、查看详情
	* @apiGroup ZhVirtualCurrency
	* @apiVersion 1.0.0
	* @apiDescription  查看详情
	
	* @apiParam (输入参数：) {string}     		id 主键ID

	* @apiHeader {String} Authorization 用户授权token
	* @apiHeaderExample {json} Header-示例:
	* "Authorization: eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOjM2NzgsImF1ZGllbmNlIjoid2ViIiwib3BlbkFJZCI6MTM2NywiY3JlYXRlZCI6MTUzMzg3OTM2ODA0Nywicm9sZXMiOiJVU0VSIiwiZXhwIjoxNTM0NDg0MTY4fQ.Gl5L-NpuwhjuPXFuhPax8ak5c64skjDTCBC64N_QdKQ2VT-zZeceuzXB9TqaYJuhkwNYEhrV3pUx1zhMWG7Org"

	* @apiParam (失败返回参数：) {object}     	array 返回结果集
	* @apiParam (失败返回参数：) {string}     	array.status 返回错误码 201
	* @apiParam (失败返回参数：) {string}     	array.msg 返回错误消息
	* @apiParam (成功返回参数：) {string}     	array 返回结果集
	* @apiParam (成功返回参数：) {string}     	array.status 返回错误码 200
	* @apiParam (成功返回参数：) {string}     	array.data 返回数据详情
	* @apiSuccessExample {json} 01 成功示例
	* {"status":"200","data":""}
	* @apiErrorExample {json} 02 失败示例
	* {"status":"201","msg":"没有数据"}
	*/
	function view(){
		$data['id'] = $this->request->get('id','','serach_in');
		$field='id,wxapp_id,name,status,create_time';
		$res  = checkData(ZhVirtualCurrencyModel::field($field)->where($data)->find());
		return $this->ajaxReturn($this->successCode,'返回成功',$res);
	}



}

